{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--BOOK_INFORMATION-->\n",
    "<img align=\"left\" style=\"padding-right:10px;\" src=\"figures/PHydro-cover-small.png\">\n",
    "*This is the Jupyter notebook version of the [Python in Hydrology](http://www.greenteapress.com/pythonhydro/pythonhydro.html) by Sat Kumar Tomer.*\n",
    "*Source code is available at [code.google.com](https://code.google.com/archive/p/python-in-hydrology/source).*\n",
    "\n",
    "*The book is available under the [GNU Free Documentation License](http://www.gnu.org/copyleft/fdl.html). If you have comments, corrections or suggestions, please send email to satkumartomer@gmail.com.*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--NAVIGATION-->\n",
    "< [Expressions](02.05-Expressions.ipynb) | [Contents](Index.ipynb) | [Functions](02.07-Functions.ipynb) >"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.6 控制流\n",
    "\n",
    "如果我们要多次执行相同的任务，只有当满足一定的条件才会限制任务的执行，而控制流就是这样做的方法。\n",
    "\n",
    "### 2.6.1 for\n",
    "\n",
    "`for`用于重复执行相同的代码。也可以用于一些列表的迭代。假设你有一些列表，你想要对列表中每项进行平方求解，并进行打印。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "25\n",
      "121\n",
      "196\n",
      "0\n",
      "36\n",
      "0\n",
      "64\n"
     ]
    }
   ],
   "source": [
    "foo = [5, 11, 14, 0, 6, 0, 8] # 定义列表\n",
    "for item in foo:\n",
    "    print(item**2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "列表中的项可以通过定义另一个具有整数的列表进行迭代，并遍历它。让我们试试这个方法来执行上面的任务："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "range(0, 7)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "foo = [5, 11, 14, 0, 6, 0, 8] # 定义列表\n",
    "a = range(7) #定义包含整数的列表\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "25\n",
      "121\n",
      "196\n",
      "0\n",
      "36\n",
      "0\n",
      "64\n"
     ]
    }
   ],
   "source": [
    "for item in a:\n",
    "    print(foo[item]**2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.6.2 while\n",
    "\n",
    "`while`语句用于当我想要持续执行某些代码，直至遇到某些情况或阻碍。假设，我们想要打印某些数字在15到20之间，我们可能这样做："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "15\n",
      "16\n",
      "17\n",
      "18\n",
      "19\n",
      "20\n"
     ]
    }
   ],
   "source": [
    "n = 15 #初始化n\n",
    "while n<=20:\n",
    "    print(n)\n",
    "    n = n + 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.6.3 if\n",
    "\n",
    "`if`语句执行某部分代码，如果条件满足，否则跳过这段代码。假设你有一些列表，你想要计算它们的倒数，但是想要跳过列表中项为零的值："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.2\n",
      "0.09090909090909091\n",
      "0.07142857142857142\n",
      "0.16666666666666666\n",
      "0.125\n"
     ]
    }
   ],
   "source": [
    "foo = [5, 11, 14, 0, 6, 0, 8] # 定义列表\n",
    "for item in foo:\n",
    "    if item is not 0:\n",
    "        print(1.0/item)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`if-else`允许代码的可选部分根据代码执行。在`if-else`中，只有一部分代码是从给定的选项中执行的。假设在前一个示例中，当列表中有零时，你想要发布一些声明。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.2\n",
      "0.09090909090909091\n",
      "0.07142857142857142\n",
      "0 found in list\n",
      "0.16666666666666666\n",
      "0 found in list\n",
      "0.125\n"
     ]
    }
   ],
   "source": [
    "foo = [5, 11, 14, 0, 6, 0, 8] # 定义列表\n",
    "for item in foo:\n",
    "    if item is not 0:\n",
    "        print(1.0/item)\n",
    "    else:\n",
    "        print('0 found in list')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`if-elif-else`被用于当你想要根据条件执行一部分代码。你可以指定任意数量的条件以及相应的代码来执行。举个例子，假设我们有一个列表，我们想要在列表中项是负数、正数或零时，打印一些语句。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "item is positive\n",
      "item is negative\n",
      "item is positive\n",
      "item is 0\n",
      "item is negative\n",
      "item is 0\n",
      "item is positive\n"
     ]
    }
   ],
   "source": [
    "foo = [5,-11,14,0,-6,0,8] #定义列表\n",
    "for item in foo:\n",
    "    if item < 0:\n",
    "        print('item is negative')\n",
    "    elif item>0:\n",
    "        print('item is positive')\n",
    "    else:\n",
    "        print('item is 0')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.6.4 break\n",
    "\n",
    "`break`语句用于中断循环。假设你想要打印列表中的所有项，但是你想要在遇到0时停止循环。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "-11\n",
      "14\n",
      "zero found in the list, stopping iterations\n"
     ]
    }
   ],
   "source": [
    "for item in foo:\n",
    "    if item==0:\n",
    "        print('zero found in the list, stopping iterations')\n",
    "        break\n",
    "    else:\n",
    "        print(item)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在你的程序遇到某些奇怪的问题时，你希望中断代码的执行，在这种情况下，`break`语句很有用。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.6.5 continue\n",
    "\n",
    "当遇到某些情况时，`continue`语句提供了一个跳出当前循环(迭代)的机会。假设你不想打印列表中为负数的项。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "14\n",
      "0\n",
      "0\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "foo = [5,-11,14,0,-6,0,8] #定义数组\n",
    "for item in foo:\n",
    "    if item<0:\n",
    "        continue\n",
    "    print(item)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.6.6 pass\n",
    "\n",
    "`pass`语句什么也不做。当语句是必要的语法时可以使用它，但程序并不需要执行任何操作。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "foo = [5,-11,14,0,-6,0,8] #定义数组\n",
    "for item in foo:\n",
    "    pass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这是经常使用的，当你正在开发你的代码，并打算以后放些东西。如果你没留下`pass`语句，Python会发出错误。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
